/*
 * @Descripttion: 请求方法
 * @version: V1.0.0
 * @Author: Shuangshuang Song
 * @LastEditors: Shuangshuang Song
 * @Date: 2020-05-23 12:57:23
 * @LastEditTime: 2020-05-30 17:00:43
 */
import fetch from '../../../static/js/common';
/**
 * 获取人员、部门、角色数据
 * @param param
 * @returns {Promise<T | never>}
 */
export function getTreeDatas(param) {
  const dic = param.dic || [];
  const ddList = [];
  // let async = true // 同步异步
  for (let i = 0; i < dic.length; i++) {
    ddList.push({
      ddCode: dic[i],
      async: param.async || false,
      rootId: param.rootId || '',
      ddName: '',
      whereSql: param.whereSql || '',
      orderSql: '',
      parentSql: '',
      nodeInfo: '',
      params: {},
      initParams: {},
      moreRoot: false,
      queryField: '',
      nodeField: '',
      formatValue: '',
      j_query: Ext.isString(param.j_query) ? param.j_query : Ext.encode(param.j_query),
    });
  }
  const params = {
    node: 'ROOT',
    onlyItem: true,
    excludes: 'checked',
    strData: JSON.stringify(ddList),
  };
  return fetch('/je/dd/tree/loadTree', {}, {
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: params,
    isFormSubmit: true,
  }).then(data => data).catch();
}

/**
 * 异步查询数据
 * @param param
 * @returns {Promise<T | never>}
 */
export function getTreeDatasAsync(param) {
  const dic = param.dic || [];
  const ddList = [];
  for (let i = 0; i < dic.length; i++) {
    ddList.push({
      ddCode: dic[i],
      async: param.async || false,
      rootId: '',
      ddName: '',
      whereSql: param.whereSql || '',
      orderSql: '',
      parentSql: '',
      nodeInfo: '',
      params: {},
      initParams: {},
      moreRoot: false,
      queryField: '',
      nodeField: '',
      formatValue: '',
      j_query: param.j_query ? (Ext.isString(param.j_query) ? param.j_query : Ext.encode(param.j_query)) : {},
    });
  }
  const params = {
    type: param.type || 'text',
    value: param.value,
    strData: JSON.stringify(ddList),
  };

  return fetch('/je/dd/tree/findAsyncNodes', {}, {
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: params,
    isFormSubmit: true,
  }).then(data => data).catch();
}

/**
 * 获取常用、工作组数据
 * @param param
 * @returns {Promise<T | never>}
 */
export function getCommonGroup(params) {
  return fetch('/je/product/crm/selector/getSelectorTree', {}, {
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: params,
    isFormSubmit: true,
  }).then(data => data).catch();
}

/**
 * 保存常用、工作组数据
 * @param param
 * @returns {Promise<T | never>}
 */
export function setCommonGroup(params) {
  return fetch('/je/product/crm/selector/addUserInfo', {}, {
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: params,
    isFormSubmit: true,
  }).then(data => data).catch();
}

/**
 * 删除工作组
 * @param param
 * @returns {Promise<T | never>}
 */
export function deleteGroup(params) {
  return fetch('/je/product/crm/selector/delWorkGroup', {}, {
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: params,
    isFormSubmit: true,
  }).then(data => data).catch();
}
